package com.winchain.chainbill.bill.biz.receive.service.impl;

import com.shell.bee.acct.AcctException;
import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.bill.base.po.CbRcv;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.biz.receive.service.BillRcvTranService;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import com.winchain.chainbill.trade.service.BillAcctService;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("billRcvTranService")
public class BillRcvTranServiceImpl implements BillRcvTranService {
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;

	protected AcctBas getAcctBas(String custId, String acctUse, String fiNo) {
		AcctBas acct = this.billAcctService.getAcctBas(custId, acctUse, fiNo);
		if (acct == null) {
			throw new AcctException(custId + "-" + acctUse + "����������");
		}
		return acct;
	}

	public AcctTxRec getAcctTxRec(CbRec record, CbRcv cbRcv, String bizType, String txAbs) {
		if (bizType == null) {
			throw new AcctException("������������������");
		}
		String billId = cbRcv.getRcvId();
		String finId = record.getFinId();
		String buyerId = cbRcv.getBuyerId();
		String sellerId = cbRcv.getSellerId();
		BigDecimal billAmt = record.getBillAmt();
		String txType = cbRcv.getTxType();

		String assetCode = "";

		String optType = "";
		String acctUse = "1";
		assetCode = AssetCode.XCB.name();
		String creditAcctType = "";
		if ("0".equals(txType)) {
			optType = "qt_inc";
			creditAcctType = "2";
		} else if ("1".equals(txType)) {
			optType = "qt_u_t";
			creditAcctType = "1";
		}
		AcctTxRec txRec = new AcctTxRec();
		txRec.setTxId(SystemUtil.getUUID());
		txRec.setTxSn(txRec.getTxId());

		txRec.setCurrCode(assetCode);
		txRec.setTxChnCode("C");

		AcctBas creditAcct = null;
		if ("0".equals(txType)) {
			creditAcct = getAcctBas(finId, creditAcctType, null);
		} else if ("1".equals(txType)) {
			creditAcct = getAcctBas(buyerId, creditAcctType, null);
		}
		txRec.setTxAcctNo(creditAcct.getAcctNo());
		txRec.setTxAcctNm(creditAcct.getAcctNm());
		txRec.setTxBankNm(creditAcct.getFiNm());

		AcctBas acct = getAcctBas(sellerId, acctUse, null);
		txRec.setCpAcctNo(acct.getAcctNo());
		txRec.setCpAcctNm(acct.getAcctNm());
		txRec.setCpBankNm(acct.getFiNm());

		AcctBkp txAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getTxAcctNo(), txRec.getCurrCode(), null, null);
		txRec.setTxBkpId(txAcctBkp.getBkpId());
		AcctBkp cpAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getCpAcctNo(), txRec.getCurrCode(), null, null);
		txRec.setCpBkpId(cpAcctBkp.getBkpId());

		txRec.setTxAmt(billAmt);
		txRec.setTxDt(SysDateUtil.getSystemDate());
		txRec.setTxTs(SysDateUtil.getSystemDateTime());
		txRec.setTxAbs(txAbs);
		txRec.setTxType(bizType);
		txRec.setOptType(optType);

		txRec.setTxSts("I");

		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);
		txRec.setBizId(billId);
		txRec.setDataScLvl("B");
		return txRec;
	}
}
